package com.dldata.devtools30.controller.metadata;

import com.dldata.devtools30.entity.DevtModelMetadataTypeEntity;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTypeService;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/DevtModelMetadataType")
public class DevtModelMetadataTypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    DevtModelMetadataTypeService metadataTypeService;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){

        List<DevtModelMetadataTypeEntity> metadataTypeEntities = metadataTypeService.getAll();

        return Result.ok(metadataTypeEntities);
    }


    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtModelMetadataTypeEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getFlmc())){
            return Result.error("分类名称不能为空");
        }

        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        metadataTypeService.save(entity);

        return Result.ok();
    }
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){

        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            metadataTypeService.deleteOne(id);

        }
        catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
}
